package com.bank.action;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.bank.pojo.BankNews;
import com.bank.svc.BankNewsSvc;
import com.bank.svc.BankNewsTypeSvc;
import com.bank.tools.JSONOutput;
import com.bank.vo.BankNewsResult;
import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
@Controller("bankAction")
public class BankNewsAction extends ActionSupport{
	@Resource(name="bankSvc")
	private BankNewsSvc bs;
	@Resource(name="newsTypeSvc")
	private BankNewsTypeSvc bnts;
	
	private String ntitle;
	private String ncontent;
	private String ntype;
	private String pageNum;
	private String id;
	private String flag;
	private String queryStr;
	
	public String addNews() throws Exception{
		BankNews news=new BankNews(bnts.getTypeById(Integer.parseInt(ntype)), ntitle, ncontent,new Timestamp(new Date().getTime()));
		bs.addNews(news);
		String json="{\"info\":\"添加成功！\"}";
		JSONOutput.OutputJSON(ServletActionContext.getResponse(), json);
		return "success";
	}
	public String getNewsByPage() throws Exception{
		List<BankNewsResult> list=bs.getNewsByPage(Integer.parseInt(pageNum));
		String json=new Gson().toJson(list);
		JSONOutput.OutputJSON(ServletActionContext.getResponse(), json);
		return "success";
	}
	public String delNews() throws Exception{
		boolean flag=bs.delNews(id);
		String json="";
		if(flag){
			json="{\"info\":\"删除成功!\"}";
		}else{
			json="{\"info\":\"删除失败!\"}";
		}
		JSONOutput.OutputJSON(ServletActionContext.getResponse(), json);
		return "success";
	}
	public String queryNews() throws Exception{
		List<BankNewsResult> list=bs.queryNews(flag, queryStr, Integer.parseInt(pageNum));
		String json=new Gson().toJson(list);
		JSONOutput.OutputJSON(ServletActionContext.getResponse(), json);
		return "success";
	}
	public String getNewsById() throws Exception{
		BankNewsResult news=bs.getNewsById(Integer.parseInt(id));
		ActionContext.getContext().getSession().put("news",news);
		return "success";
	}
	public String updateNews() throws Exception{
		BankNews news=new BankNews(Integer.parseInt(id),bnts.getTypeById(Integer.parseInt(ntype)), ntitle, ncontent,
									new Timestamp(new Date().getTime()));
		bs.updateNews(news);
		String json="{\"info\":\"修改成功！\"}";
		JSONOutput.OutputJSON(ServletActionContext.getResponse(), json);
		return "success";
	}
	
	public void setNtitle(String ntitle) {
		this.ntitle = ntitle;
	}
	public void setNcontent(String ncontent) {
		this.ncontent = ncontent;
	}
	
	public void setNtype(String ntype) {
		this.ntype = ntype;
	}
	public void setBs(BankNewsSvc bs) {
		this.bs = bs;
	}
	public void setBnts(BankNewsTypeSvc bnts) {
		this.bnts = bnts;
	}
	public void setPageNum(String pageNum) {
		this.pageNum = pageNum;
	}
	public void setId(String id) {
		this.id = id;
	}
	public void setFlag(String flag) {
		this.flag = flag;
	}
	public void setQueryStr(String queryStr) {
		this.queryStr = queryStr;
	}
}
